# -*- coding: UTF-8 -*-
# @Project ：big-data 
# @File    ：故宫感情评论.py
# @Author  ：于金龙
# @IDE     ：PyCharm 
# @Date    ：2024/4/26 16:06
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from wordcloud import WordCloud, STOPWORDS
import jieba
from jieba import analyse

plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

data = pd.read_csv('故宫评论情感 .csv')
print(data)

# 绘制感情直方图

bins = np.arange(0, 1.1, 0.1)

plt.hist(data['emotion'], bins, color='#4F94CD', alpha=0.9)
plt.xlim(0, 1)
plt.xlabel("数量")
plt.title('情感分布直方图')

# 词云图
stop_words = set(STOPWORDS)  # 添加停用词
stop_words.add('的')
stop_words.add('了')
myfont = r'D:\gitee代码托管\big-data\数据分析-web\msyh.ttc'
w = WordCloud(font_path=myfont,
              height=400, width=700, stopwords=stop_words)
text = ''
for i in data['评论内容']:
    text += i
data_cut = ''.join(jieba.lcut(text))
w.generate(data_cut)
image = w.to_file('词云图.png')

# 消极评论和积极评论
pos, neg = 0, 0
for i in data['emotion']:
    if i >= 0.5:
        pos += 1
    else:
        neg += 1
print('积极评论数目为：', pos)
print('消极评论数目为：', neg)

# 扇形图
pie_labels = 'positive', 'negative'
pie = plt.pie([pos, neg], labels=pie_labels, autopct='%1.2f', shadow=True)
plt.savefig('扇形图.png')

data2 = data[data['emotion'] < 0.4]
data2.head()

# 消极评论关键词top10
text2 = ''
for s in data2['评论内容']:
    text2 += s

key_words = jieba.analyse.extract_tags(sentence=text2, topK=10, withWeight=True, allowPOS=())
print(key_words)
